/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-03 00:36:50
 */
import { time } from 'console';
import { BufferAttribute, Color, LinearFilter, LinearSRGBColorSpace, MeshBasicMaterial, NoBlending, RGBAFormat, ShaderChunk, ShaderMaterial, SRGBColorSpace, Vector2, WebGLRenderTarget } from 'three';
import ThreeBase from 'three-base';
import FullScreenQuad from 'three-base/pass/FullScreenQuad';

/**
 *
 *
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {
  constructor() {
    super();
  }

  ringRenderTarget = new WebGLRenderTarget(128, 128, { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat });

  setSize(width: number, height: number): void {
    super.setSize(width, height);
    this.ringRenderTarget.setSize(width * window.devicePixelRatio, height * window.devicePixelRatio);
  }

  shader = new ShaderMaterial({
    uniforms: {
      resolution: { value: new Vector2(100, 100) },
      ringColor: { value: new Color('#ffffff') }, // 光环的颜色
      bg: { value: new Color('#F5821F') }, // 背景色
      radiuss: { value: new Array(50).fill(10) }, // 半径数组
    },
    vertexShader: `
        ${ShaderChunk.common}
        ${ShaderChunk.logdepthbuf_pars_vertex}
        varying vec2 vUv;
        void main() {
            vUv = uv;
            // gl_Position = vec4(position, 1.0);
            // gl_Position = projectionMatrix * modelViewMatrix * instanceMatrix * vec4(position, 1.0);
            gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
            ${ShaderChunk.logdepthbuf_vertex}
        }
    `,
    fragmentShader: `
        ${ShaderChunk.logdepthbuf_pars_fragment}
        uniform vec3 bg;
        uniform vec3 ringColor;
        uniform vec2 resolution;
        varying vec2 vUv;
        uniform float radiuss[50];

        void main() {

            vec2 offset = vec2( (resolution.x - 1.0)/2.0, (resolution.y - 1.0)/2.0);
            float d = distance(vUv * resolution - offset, vec2(0.5));
            float opacity = 0.0;

            float width = 0.03;
            for(int i= 0; i < 50; i++){
               opacity = max(opacity, smoothstep(width,0.0, abs(d - radiuss[i])) / 1.2 );
            }
            gl_FragColor = vec4(bg, 0.7) + vec4( ringColor * opacity, 0.0);
            ${ShaderChunk.logdepthbuf_fragment}
        }
    `,
    blending: NoBlending, // 渲染时不与背景融合计算 直接替换
    depthTest: true,
    depthWrite: true,
    transparent: true,
  });

  init() {
    super.init();
    this.renderer.domElement.addEventListener('click', () => {
      const radiuss = [...this.shader.uniforms.radiuss.value];
      const maxNumber = Math.max(...radiuss);
      // 找到最大数的索引
      const index = radiuss.indexOf(maxNumber);
      // 将最大数更新为0
      if (index !== -1) {
        radiuss[index] = 0;
      }
      this.shader.uniforms.radiuss.value = radiuss;
    });
  }

  // 全屏渲染模型
  fullScreenMesh = new FullScreenQuad();

  render(): void {
    this.shader.uniforms.resolution.value.set(this.size.x / this.size.y, 1);
    this.fullScreenMesh.render(this.renderer, this.shader);

    const radiuss = [...this.shader.uniforms.radiuss.value];
    this.shader.uniforms.radiuss.value = radiuss.map((v) => (v += 0.03));
  }
}

const homeRenderer = (window.homeRenderer = new HomeRender());

export default homeRenderer;
